package com.tina.controller;


import cn.hutool.core.img.ImgUtil;
import com.tina.entity.TiSlideshow;
import com.tina.entity.vo.TiSlideshowVo;
import com.tina.mapper.TiSlideshowMapper;
import com.tina.model.ResultFormat;
import com.tina.util.ImageUtil;
import com.tina.util.ResultUtil;
import com.tina.util.TimeUtitl;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 画廊 前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/tiSlideshow/")
public class TiSlideshowController {
    @Autowired
    private TiSlideshowMapper slideshowMapper;

    private Logger logger = LoggerFactory.getLogger(TiSlideshowController.class);

    @GetMapping("querySlideshow")
    public Map<String, Object> querySlideshow() {
        List<TiSlideshowVo> tiSlideshows = slideshowMapper.querySlideshow();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "Images");
        map.put("start", 0);
        map.put("title", "画廊管理");
        map.put("data", tiSlideshows);
        return map;
    }

    @PutMapping("delSlideshow")
    public ResultFormat delSlideshow(TiSlideshow slideshow) {
        logger.info("slideshow={}", slideshow);
        slideshow.setSlideshowState(1);
        logger.info("slideshow={}", slideshow);
        Integer updateById = slideshowMapper.updateById(slideshow);
        if (updateById > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * 批量上传图片
     * @return
     */
    @PostMapping("uploadNewImg")
    public ResultFormat uploadNewImg(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String prefix = "";
            String dateStr = "";
            String originalName = file.getOriginalFilename();
            prefix = originalName.substring(originalName.lastIndexOf(".") + 1);
            logger.info("originalName={}", originalName);
            logger.info("prefix={}", prefix);
            boolean image = ImageUtil.isImage(prefix);
            logger.info("判断格式={}", image);
            if (image) {
                long startTime = System.currentTimeMillis();
                logger.info("文件类型={}", file.getContentType());
                logger.info("文件大小={}", file.getSize());
                logger.info("文件原名称={}", file.getOriginalFilename());
                dateStr = startTime + "." + prefix;
                FileUtils.copyInputStreamToFile(file.getInputStream(), new File("/usr/apk/apache-tomcat-9.0.22/webapps/images/", dateStr));
                TiSlideshow slideshow = new TiSlideshow();
               String thum = startTime + "-thum." + prefix;
               logger.info("prefix={}",prefix);
               logger.info("thum={}",thum);
                Thumbnails.of("/usr/apk/apache-tomcat-9.0.22/webapps/images/" + dateStr)
                        .sourceRegion(Positions.CENTER,360,204)
                        .size(360,204)
                        .toFile("/usr/apk/apache-tomcat-9.0.22/webapps/images/" + thum);
                slideshow.setSlideshowState(0);
                slideshow.setSlideshowName(file.getOriginalFilename());
                slideshow.setSlideshowImg("http://pigs.gold:8762/images/" + dateStr);
                slideshow.setSlideshowThumb("http://pigs.gold:8762/images/" +thum);
                slideshow.setSlideshowTime(TimeUtitl.dateTime());
                Integer insert = slideshowMapper.insert(slideshow);
                if (insert > 0) {
                    logger.info("上传成功");
                    return ResultUtil.success(slideshow);
                }
            }
        }
        return ResultUtil.error();
    }

}
